Ext.define('XTFrame.controller.ruiyuan.News', {
	extend: 'Ext.app.Controller',
	init: function(){
		this.control({
			'ruiyuanNewsEditWindow xtframeIframe': {
				iframeOnload: function (o){
					var form = o.up('ruiyuanNewsEditWindow').down('form').getForm();
					o.iframe.setContent(form.findField("content").getValue());
				}
			},
			'ruiyuanNewsManager': {
				beforeupdate: function(grid, win, mark, opt, row){
//					if(!Ext.isEmpty(grid.editWindowObject)){
//						var ifr = grid.editWindowObject.down('xtframeIframe');
//						if(mark == 'edit'){
//							ifr.iframe.setContent(row.data.content);
//						} else if(mark == 'add') {
//							ifr.iframe.setContent("");
//						}
//					}
					
					var panel, form, ifr;
					if(mark=='add' || mark=='edit'){
						var params = grid.up('panelBorder').params;
						if(Ext.isEmpty(params) || params==null){
							Ext.xtframe.msg("提示", "内容加载失败！");
							opt.flag = false;
							return;
						}
						
						win = viewFactory.getWindow(grid.editWinId, grid.editWindow);
						var colData = this.getColumnData(params.cls);
						if(colData == null){
							Ext.xtframe.msg("提示", "内容加载失败！");
							opt.flag = false;
							return;
						}
						win.show();
						ifr = win.down('xtframeIframe');
						if(ifr.iframe != null){
							if(mark == 'edit'){
								ifr.iframe.setContent(row.data.content);
							} else if(mark == 'add') {
								ifr.iframe.setContent("");
							}
						}
						panel = win.down('form');
						form = panel.getForm();
						form.findField("col_id").getStore().loadData(colData);
					} else if(mark == 'editsave'){
						ifr = win.down('xtframeIframe');
						form = win.down('form').getForm();
						var htmlstr = ifr.iframe.getContent();
						form.findField("content").setValue(htmlstr);
						var imgs = this.getImgSrc(htmlstr);
						if(imgs.length>0 && form.findField("subject_img").getValue().trim().length<1){
							form.findField("subject_img").setValue(imgs[0]);
						}
						
						if(win.flag=='add'){
							var serverData = ajaxUtil.serverData("regid","ry_newsid");
							if(ajaxUtil.isSucceed(serverData)){
								var newsid = serverData.retData;
								form.findField("news_id").setValue(newsid);
							} else {
								Ext.xtframe.msg("提示", "保存失败！");
								opt.flag = false;
							}
						}
					}
				}
			}
		});
	},
	colData: null,
	getColumnData: function(cls){
		var data = ajaxUtil.executeQuery('ruiyuan_news_8', {cls:cls});
		if(ajaxUtil.isSucceed(data)){
			this.colData = data.arrData;
		} else {
			Ext.xtframe.msg("提示", "数据加载失败");
		}
		return this.colData;
	},
	getImgSrc: function(htmlstr){
		var reg=/<img.+?src=('|")?([^'"]+)('|")?(?:\s+|>)/gim;
		var arr = [];
		while(tem=reg.exec(htmlstr)){
			arr.push(tem[2]);
		}
		return arr;
	}
});